import 'dart:convert';
import 'dart:core';
import 'dart:ui';

import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/utils/sp_utils.dart';

import 'live_chat_entity.dart';

enum MessageType {
  joinGroup(value: 0, color: Colours.orangeFCDDA1, matchColor: Colours.grey99),
  sendMessage(value: 1),
  deleteMessage(value: 2),
  mute(value: 3),
  muteAll(value: 4),
  cancelMute(value: 5),
  cancelMuteAll(value: 6),
  exitGroup(value: 7),
  focus(
      value: 8, color: Colours.orangeFFA800, matchColor: Colours.orangeFFA800),
  gift(value: 9, color: Colours.orangeFFA800, matchColor: Colours.orangeFFA800),
  announce(value: 77, color: Color(0xFFF56060), matchColor: Color(0xFFF55F5F)),
  completion(value: 99),
  other(
      value: 888,
      color: Colours.orangeFFA800,
      matchColor: Colours.orangeFFA800),
  error(value: 999);

  final int value;
  final Color color;
  final Color matchColor;

  const MessageType(
      {required this.value,
      this.color = Colours.white,
      this.matchColor = Colours.text});

  static MessageType? getType(int value) {
    for (MessageType type in MessageType.values) {
      if (type.value == value) {
        return type;
      }
    }
    return null;
  }
}

class AliLiveMessage {
  String? message;
  String? messageId;
  String? groupId;
  MessageType? status;
  String? userAvatar;
  String? userId;
  String? userName;
  String? animateUrl;
  String? giftLogo;
  String? giftName;
  double? price;
  int? count;
  DateTime? showTime;

  AliLiveMessage(
      {this.message,
      this.messageId,
      this.groupId,
      this.status,
      this.userAvatar,
      this.userId,
      this.userName,
      this.price,
      this.showTime});

  AliLiveMessage.fromJson(Map<String, dynamic> json) {
    status = MessageType.getType(json['status']);
    if (status == MessageType.focus) {
      message = "关注了主播";
    } else if (status == MessageType.joinGroup) {
      message = "进入直播间";
    } else if (status == MessageType.sendMessage) {
      if (json['message'] != null) {
        Json data = jsonDecode(json['message']);
        message = data['message'];
      }
    } else if (status == MessageType.gift) {
      if (json['message'] != null) {
        Json data = jsonDecode(json['message']);
        message = data['message'];
        giftLogo = data['logo'];
        giftName = data['giftName'];
        animateUrl = data['specialEffect'];
        count = data['count'];
        price = data['price'];
      }
    } else if (status == MessageType.other) {
      try {
        if (json['message'] != null) {
          Json data = jsonDecode(json['message']);
          message = data['message'];
        }
      } catch (e) {
        message = json['message'];
      }
    } else {
      message = json['message'];
    }
    messageId = json['messageId'];
    groupId = json['groupId'];
    userAvatar = json['userAvatar'];
    userId = json['userId'];
    userName = json['userName'];
  }

  AliLiveMessage.fromChatMessage(LiveChatEntity? chatEntity) {
    if (chatEntity?.type == ChatMessageType.comment) {
      status = MessageType.sendMessage;
    } else if (chatEntity?.type == ChatMessageType.focus) {
      status = MessageType.focus;
    } else if (chatEntity?.type == ChatMessageType.enter) {
      status = MessageType.joinGroup;
    } else if (chatEntity?.type == ChatMessageType.gift) {
      status = MessageType.gift;
    } else {
      status = MessageType.other;
    }
    messageId = "${chatEntity?.id ?? ""}";
    message = chatEntity?.message;
    userId = chatEntity?.userId;
    userName = chatEntity?.userName;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['message'] = this.message;
    data['messageId'] = this.messageId;
    data['groupId'] = this.groupId;
    data['status'] = this.status?.value;
    data['userAvatar'] = this.userAvatar;
    data['userId'] = this.userId;
    data['userName'] = this.userName;
    return data;
  }
}
